/*
 * @(#)StateSynRestart.java   1.0  2021年12月8日
 * 
 * Copyright (c)	2014-2020. All Rights Reserved.	GuangZhou hhmk Technology Company LTD.
 */
package com.swift.dts.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.swift.core.auth.Login;
import com.swift.core.model.ServiceRequest;
import com.swift.core.model.data.DataModel;
import com.swift.core.service.SynInterface;
import com.swift.core.validator.DataValidator;
import com.swift.core.validator.annotation.ParamNotNull;
import com.swift.dts.dao.model.SynObjectKey;
import com.swift.dts.offline.OfflineLifeCycle;
import com.swift.exception.ServiceException;

/**
 * 添加说明 
 * @author zhengjiajin
 * @version 1.0 2021年12月8日
 */
@Service("stateSynRestart")
@Login
public class StateSynRestart implements SynInterface{

    @Autowired
    private OfflineLifeCycle offlineLifeCycle;
    /** 
     * @see com.swift.core.service.SynInterface#doService(com.swift.core.model.ServiceRequest)
     */
    @Override
    @DataValidator(NotNull = { 
        @ParamNotNull(param = "dataSource", message = "dataSource不能为空"),
        @ParamNotNull(param = "destination", message = "destination不能为空"),
        @ParamNotNull(param = "dbName", message = "dbName不能为空"),
        @ParamNotNull(param = "tableName", message = "tableName不能为空"),
        })
    public DataModel doService(ServiceRequest req) {
        SynObjectKey synObjectKey = new SynObjectKey();
        synObjectKey.setDataSource(req.getData().getString("dataSource"));
        synObjectKey.setDestination(req.getData().getString("destination"));
        synObjectKey.setDbName(req.getData().getString("dbName"));
        synObjectKey.setTableName(req.getData().getString("tableName"));
        if(offlineLifeCycle.onRunOffline(synObjectKey)) {
            throw new ServiceException("离线任务还在执行中,禁止再次执行");
        }
        offlineLifeCycle.start(synObjectKey);
        return null;
    }

}
